package com.dkm.nettysocketdemo.untils;

import com.alibaba.druid.util.StringUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.UUID;

/**
 * @ClassName : BarcodeUtil  //类名
 * @Description : 条形码OR二维码工具类  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-12-09 16:17  //时间
 */
public class BarcodeUtil {
    /**
     * 生成文件
     *
     * @param msg
     * @param path
     * @return
     */
    public static File generateFile(String msg, String path) {
        File file = new File(path);
        try {
            generate(msg, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return file;
    }

    /**
     * 生成字节
     *
     * @param msg
     * @return
     */
    public static byte[] generate(String msg) {
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        generate(msg, ous);
        return ous.toByteArray();
    }

    /**
     * 生成到流
     *
     * @param msg
     * @param ous
     */
    public static void generate(String msg, OutputStream ous) {
        if (StringUtils.isEmpty(msg) || ous == null) {
            return;
        }

        Code39Bean bean = new Code39Bean();

        // 精细度
        final int dpi = 150;
        // module宽度
        final double moduleWidth = UnitConv.in2mm(1.0f / dpi);

        // 配置对象
        bean.setModuleWidth(moduleWidth);
        bean.setWideFactor(3);
        bean.doQuietZone(false);

        String format = "image/png";
        try {

            // 输出到流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
                    BufferedImage.TYPE_BYTE_BINARY, false, 0);

            // 生成条形码
            bean.generateBarcode(canvas, msg);

            // 结束绘制
            canvas.finish();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String msg = "123456789";
//        String path = "barcode.png";
        String path = "G:/Image/barcode.png";
        generateFile(msg, path);
        System.out.println("条形码生成==" + generateFile(msg, path));
    }

    public static String QRcode(String urlCode) throws WriterException, IOException {
        //需要编码的内容
        QRCodeWriter qrCodeWriter1 = new QRCodeWriter();
        //设置二维码图片宽高
        BitMatrix bitMatrix1 = qrCodeWriter1.encode(urlCode, BarcodeFormat.QR_CODE,600, 600);
        String pngName = "mnt/data/hbats/qrcode-upload/"+UUID.randomUUID().toString().replaceAll("-","")+".png";
        //输出到指定路径
        Path path = FileSystems.getDefault().getPath(pngName);
        MatrixToImageWriter.writeToPath(bitMatrix1,"PNG",path);
        // 写到输出流
        ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix1, "PNG", outputStream1);
        //转换为base64
        /*Base64.Encoder encoder1 = Base64.getEncoder();
        String advUrl = "data:image/jpeg;base64,"
                +encoder1.encodeToString(outputStream1.toByteArray());
        //打印base64结果
        System.out.println(advUrl);*/

        return pngName;
    }
}
